<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>13_综合案例</title>
    <style>
        #app {
            margin: 0 auto;
            width: 500px;
            height: 500px;
        }
        
        table {
            border-collapse: collapse;
        }
        
        table,
        th,
        td {
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <thead>
                <th></th>
                <th>书籍名称</th>
                <th>出版日期</th>
                <th>价格</th>
                <th>购买数量</th>
                <th>操作</th>
            </thead>

            <tbody>
                <tr v-for="item,index in books" :key="index">
                    <td>{{index + 1}}</td>
                    <td>{{item.bookName}}</td>
                    <td>{{item.bookDate}}</td>
                    <!-- 写个过滤器: showPrice:只保留两位小数,并拼接上 ¥ -->
                    <td>{{item.price | showPrice}}</td>
                    <td>
                        <!-- 当书本数量为1时,让减少书本数量的按钮处于disabled状态 -->
                        <button @click="decrement(index)" v-bind:disabled="item.count <= 1">-</button> {{item.count}}
                        <button @click="increment(index)">+</button>
                    </td>
                    <td>
                        <button @click="removeBook">移除</button>
                    </td>
                </tr>

            </tbody>
        </table>
        <p>总价:{{totalPrice}}</p>
    </div>
</body>
<script src="../vue.js"></script>
<script>
    const vm = new Vue({
        el: '#app',
        data: {
            books: [{
                bookName: "西游记",
                bookDate: '2020-01-01',
                price: 99.9911,
                count: 1
            }, {
                bookName: "红楼梦",
                bookDate: '2020-02-02',
                price: 88.88,
                count: 1
            }, {
                bookName: "水浒传",
                bookDate: '2020-03-03',
                price: 77.99,
                count: 1
            }, {
                bookName: "三国演义",
                bookDate: '2020-04-04',
                price: 100.99,
                count: 1
            }]
        },
        methods: {
            increment(index) {
                this.books[index].count += 1
            },
            decrement(index) {
                this.books[index].count -= 1
            },
            removeBook(index) {
                this.books.splice(index, 1)
            }
        },
        computed: {
            totalPrice() {
                let res = 0
                for (let i in this.books) {
                    res += this.books[i].price * this.books[i].count
                }
                return res.toFixed(2)
            }
        },
        filters: {
            showPrice(price) {
                return '¥' + price.toFixed(2)
            }
        }
    })
</script>

</html>